package com.hzhr.iot.statis.util;

public class StrUtil {
	public static String replaceLast(String text, String regex, String replacement) {
        return text.replaceFirst("(?s)"+regex+"(?!.*?"+regex+")", replacement);
    }

	public static String obj2str(Object object) {
		if (object == null) return null;
		return object.toString();
	}

	/**
	 * 字符串左补0
	 * @param str 原始字符串
	 * @param size 补充0之后的长度
	 */
	public static String FillZeroForLeft(String str, int size) {
		int strSize =  str.length();
		if (strSize < size) {
			int zeroCount = size - strSize;
			for (int i = 0; i < zeroCount; i++) {
				str = "0" + str;
			}
		}
		return str;
	}
	
	/**
	 * 首字母大写
	 */
	public static String firstCharToUppserCase(String str) {
		StringBuilder sb = new StringBuilder();
		char[] cs = str.toCharArray();
		
		for (int i = 0; i < cs.length; i++) {
			char c = cs[i];
			if (i == 0) {
				sb.append(Character.toUpperCase(c));
			} else {
				sb.append(c);
			}
		}
		
		return sb.toString();
	}
	
	/**
	 * 首字母小写
	 */
	public static String firstCharToLowerCase(String str) {
		StringBuilder sb = new StringBuilder();
		char[] cs = str.toCharArray();
		
		for (int i = 0; i < cs.length; i++) {
			char c = cs[i];
			if (i == 0) {
				sb.append(Character.toLowerCase(c));
			} else {
				sb.append(c);
			}
		}
		
		return sb.toString();
	}
	
	/**
	 * 字符串转小写
	 * @title strToLowerCase
	 * @description
	 * @param str
	 * @return
	 * @author 48310
	 * @date 2018年4月10日
	 * @see 
	 *
	 */
	public static String strToLowerCase(String str) {
		StringBuilder sb = new StringBuilder();
		char[] cs = str.toCharArray();
		
		for (int i = 0; i < cs.length; i++) {
			char c = cs[i];
			if (Character.isUpperCase(c)) {
				sb.append(Character.toLowerCase(c));
			} else {
				sb.append(c);
			}
		}
		
		return sb.toString();
	}
	
	/**
	 * 隔断驼峰字符串
	 */
	public static String splitHumpStr(String str, String regex) {
		StringBuilder sb = new StringBuilder();
		char[] cs = str.toCharArray();
		
		for (int i = 0; i < cs.length; i++) {
			char c = cs[i];
			if (Character.isUpperCase(c)) {
				sb.append(regex + Character.toLowerCase(c));
			} else {
				sb.append(c);
			}
		}
		
		return sb.toString();
	}
	
	public static void main(String[] args) {
		System.out.println(splitHumpStr("AaA", "-"));
	}
}
